const express = require("express");
const pool = require("../pool.js");

const docRouter = express.Router();
module.exports = docRouter;
// 根据医师账号查找
docRouter.get("/find", (req, res,next) => {
  var docname = req.query.docname;
  var sql = `select * from doctor where docname=?`;
  pool.query(sql, [docname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, data: r });
  });
});
// 查找整个医师表
docRouter.get("/findAll", (req, res,next) => {
  var page = req.query.page || 1;
  const pagesize = 10;
  var offset = (page - 1) * pagesize;
  var sql = `select count(docid) as count from doctor`;
  pool.query(sql, (err, r) => {
    if (err) {
      next(err);
      return;
    }
    let pagecount = Math.ceil(r[0].count / pagesize);
    var sql = `select * from doctor limit ?,?`;
    pool.query(sql, [offset, pagesize], (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, data: r, pagecount });
    });
  });
});
// 医师注册
docRouter.post("/register", (req, res,next) => {
  var docname = req.body.docname;
  var realname = req.body.realname;
  var docpwd = req.body.docpwd;
  var docphone = req.body.docphone;
  var docsymptoms = req.body.docsymptoms;
  var doctime = req.body.doctime;
  if (
    docname == "" ||
    realname == "" ||
    docpwd == "" ||
    docsymptoms == "" ||
    doctime == ""
  ) {
    res.send({ code: 202, msg: "输入不可为空" });
  }
  if (!/^\w{6,10}$/.test(docname)) {
    res.send({ code: 203, msg: "账号格式不正确" });
    return;
  }
  if (!/^\w{6,12}$/.test(docpwd)) {
    res.send({ code: 204, msg: "密码格式不正确" });
    return;
  }
  if (docphone != "" && !/^1[3-9]\d{9}$/.test(docphone)) {
    res.send({ code: 205, msg: "手机号格式不正确" });
    return;
  }
  var sql1 = `select docid from doctor where docname=?`;
  pool.query(sql1, [docname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      res.send({ code: 201, msg: "医师已存在" });
    } else {
      var sql2 = `insert into doctor(docid,docname,realname,docpwd,docphone,docsymptoms,doctime) values(replace(UUID(),'-',''),?,?,MD5(?),?,?,?)`;
      pool.query(
        sql2,
        [docname, realname, docpwd, docphone, docsymptoms, doctime],
        (err, r, next) => {
          if (err) {
            next(err);
            return;
          }
          res.send({ code: 200, msg: "注册成功" });
        }
      );
    }
  });
});
// 医师登录
docRouter.post("/login", (req, res,next) => {
  var docname = req.body.docname;
  var docpwd = req.body.docpwd;
  var sql = `select realname from doctor where docname=? and docpwd=MD5(?)`;
  pool.query(sql, [docname, docpwd], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      res.send({ code: 200, msg: "登录成功", data: r });
    } else {
      res.send({ code: 201, msg: "登录失败" });
    }
  });
});
// 删除医师数据
docRouter.delete("/delete", (req, res,next) => {
  var docname = req.query.docname;
  var sql = `delete from doctor where docname=?`;
  pool.query(sql, [docname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (!r.affectedRows) {
      res.send({ code: 201, msg: "无此医师" });
    } else {
      res.send({ code: 200, msg: "删除成功" });
    }
  });
});
// 医师个人信息修改
docRouter.put("/update", (req, res,next) => {
  var docname = req.body.docname;
  var realname = req.body.realname;
  var docpwd = req.body.docpwd;
  var docimg = req.body.docimg;
  var license = req.body.license;
  var docsymptoms = req.body.docsymptoms;
  var doctime = req.body.doctime;
  var docphone = req.body.docphone;
  var docstate = req.body.docstate;
  if (
    docname == "" ||
    realname == "" ||
    docpwd == "" ||
    docsymptoms == "" ||
    doctime == ""
  ) {
    res.send({ code: 202, msg: "输入不可为空" });
    return;
  }
  if (!/^\w{6,12}$/.test(docpwd)) {
    res.send({ code: 203, msg: "密码格式不正确" });
    return;
  }
  if (docphone != "" && !/^1[3-9]\d{9}$/.test(docphone)) {
    res.send({ code: 204, msg: "手机号格式不正确" });
    return;
  }
  var sql = `update doctor set realname=?, docpwd=MD5(?),docimg=?,license=?,docsymptoms=?,doctime=?,docphone=?,docstate=? where docname=?`;
  pool.query(
    sql,
    [
      realname,
      docpwd,
      docimg,
      license,
      docsymptoms,
      doctime,
      docphone,
      docstate,
      docname,
    ],
    (err, r) => {
      if (err) {
        next(err);
        return;
      }
      if (!r.affectedRows) {
        res.send({ code: 201, msg: "更新失败" });
      } else {
        res.send({ code: 200, msg: "更新成功" });
      }
    }
  );
});
